package demo.oss;

import org.springframework.util.Base64Utils;

import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class connect {

//    public static void main(String[] args) {
//        String str = "ni/ha/o/";
//        System.out.println(str);
//        System.out.println(str.charAt(str.length()-1) == '/');
//        System.out.println(str.matches("(.*)/"));
//    }


    /*
    http://image-demo.img-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,size_40,text_MTAwODY=,rotate_330,t_50,g_ne
    http://image-demo.img-cn-hangzhou.aliyuncs.com/panda.png
     */

    public static void main(String[] args) {
//        String ossUrl = "https://resource.oss.yatiku.com/activity/nurse/mixunyati/20180829/sc/nurse_mixunyati_0829_sc_17.jpg";
//        String ossUrl = "https://resource.oss.yatiku.com/fileuser/timonials/223/img/1533113256370.jpg";
//        String ossUrl = "https://resource.oss.yatiku.com/fileuser/timonials/187/img/1533118162958.jpg";
//        String ossUrl = "https://resource.oss.yatiku.com/fileuser/timonials/235/img/1537145981045.jpg?x-oss-process=image/rotate,270";
//        String ossUrl = "https://resource.oss.yatiku.com/activity/nurse/mixunyati/20180829/sc/nurse_mixunyati_0829_sc_17.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s=,size_40,text_6bit6aKY5bqT77yaMTIzNDU=,rotate_315,t_50,g_center";
//        String text = "鸭题库 1234567";
//        System.out.println(ossUrlAddWatermark(ossUrl, text));
//        System.out.println(parseLength(10086,8));
//        System.out.println(getInnerInt("123"));
        System.out.println(insertStringInParticularPosition("test","abc",3));
    }

    private static String ossUrlAddWatermark(String ossUrl, String text) {
        text = Base64Utils.encodeToString(text.getBytes());
        if (!ossUrl.contains("?x-oss-process=image")) {
            if (!ossUrl.contains("resource.oss.yatiku.com")) {
                return ossUrl;
            }
            ossUrl = ossUrl + "?x-oss-process=image";
        }
        if (ossUrl.contains("/watermark")) {
            return ossUrl;
        } else {
            ossUrl = ossUrl + "/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s=,size_50,text_" + text + ",rotate_330,t_60,g_center,color_FFFFFF";
        }
        return ossUrl;
    }

    private static String getType(int type) {
        switch (type) {
            case 1:
                return "d3F5LXplbmhlaQ==";
            case 2:
                return "d3F5LW1pY3JvaGVp";
            case 3:
                return "ZmFuZ3poZW5nc2h1c29uZw==";
            case 4:
                return "ZmFuZ3poZW5na2FpdGk=";
            case 5:
                return "ZmFuZ3poZW5naGVpdGk=";
            case 6:
                return "ZmFuZ3poZW5nZmFuZ3Nvbmc=";
            case 7:
                return "ZHJvaWRzYW5zZmFsbGJhY2s=";
            default:
                return "d3F5LXplbmhlaQ==";
        }
    }

    public static String parseLength(int number,int length){
        StringBuilder r = new StringBuilder(String.valueOf(number));
        while (r.length() < length) {
            r.insert(0, "0");
        }
        return r.toString();
    }


    public static int getInnerInt (String text){
//        // 要验证的字符串
//        String str = "baike.xsoftlab.net";
//        // 正则表达式规则
//        String regEx = "baike.*";
//        // 编译正则表达式
//        Pattern pattern = Pattern.compile(regEx);
//        // 忽略大小写的写法
//        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
//        Matcher matcher = pattern.matcher(str);
//        // 查找字符串中是否有匹配正则表达式的字符/字符串
//        boolean rs = matcher.find();
//        System.out.println(rs);

        String s = "AB125B369C658B";
        // 把要匹配的字符串写成正则表达式，然后要提取的字符使用括号括起来
        // 在这里，我们要提取最后一个数字，正则规则就是“一个数字加上大于等于0个非数字再加上结束符”
        Pattern pattern = Pattern.compile("(B)-?\\d+");
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            text = matcher.group(0);
        } else {
            text = "0";
        }
        return Integer.valueOf(text.substring(2));
    }

    public static String insertStringInParticularPosition(String src, String dec, int position){
        StringBuffer stringBuffer = new StringBuffer(src);
        return stringBuffer.insert(position, dec).toString();
    }
}
